package top.dancun.easstudent.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.dancun.common.entity.UserEntity;
import top.dancun.common.utils.DataUtils;
import top.dancun.common.utils.PageUtils;
import top.dancun.common.utils.R;
import top.dancun.easstudent.entity.StudentEntity;
import top.dancun.easstudent.feign.SysFeign;
import top.dancun.easstudent.feign.service.SysFeignService;
import top.dancun.easstudent.service.StudentService;

import java.util.*;

/**
 * @author dancun
 */
@RestController()
@RequestMapping("/student")
public class StudentController extends AbstractController {
    @Autowired
    private StudentService studentService;



    /**
     * 信息
     */

    @GetMapping("/info/{id}")
    @RequiresPermissions("easstudent:student:info")
    public  R getStudent(@PathVariable String id){
        StudentEntity student = studentService.getById(id);

        return R.ok().put("student",student);
    }

    @GetMapping("/info")
    public  R getStudentInfo(){
        String uid = getUserId();
        return R.ok().put("data",studentService.showAllInfo(uid));
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("easstudent:student:list")
    public R list(@RequestBody Map<String, Object> params){
        Map<String,String> map = new HashMap<>();
        String[] fields = new String[]{"classCode","name","deptName","majorName","sId"};
        DataUtils.mapAddNoEmpty(map,params,fields);
        // 必须同时判断空和空字符串


        PageUtils page = studentService.queryPage(params,map);


        return R.ok().put("page", page);
    }
    @RequestMapping("/listname")
    @RequiresPermissions("easstudent:student:list")
    public R listName(){
       
        // 必须同时判断空和空字符串


        List<StudentEntity> data = studentService.list();

        return R.ok().put("data", data);
    }




    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("easstudent:student:save")
    public R save(@RequestBody StudentEntity student){
        studentService.save(student);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("easstudent:student:update")
    public R update(@RequestBody StudentEntity student){
        studentService.updateById(student);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("easstudent:student:delete")
    public R delete(@RequestBody String[] sids){
        studentService.removeByIds(Arrays.asList(sids));

        return R.ok();
    }

    /**
     *
     * @param params 分页信息
     * @param ids 学生id
     * @param cids 班级id
     * @return
     */
    @RequestMapping("/getbyids")
//    @RequiresPermissions("easbaseinfo:student:getbyids")
    public R getByIds(@RequestParam(required = false) List<String> ids, @RequestParam(required = false)List<Integer> cids,@RequestParam(required = false) Map<String,Object> params ){
        PageUtils list = studentService.getByIds(params,ids,cids);

        return R.ok().put("page",list);
    }
}
